﻿==============================================================================  
// Created by Bndy at 2010/3/18
// Copyright (c) 2010 Bndy.Net, All rights reserved.  
//  
//                    * * * * * * * * * * * * *   
//                    * MAIL- bndy5337@163.com *       http://www.bndy.net  
//                    * Q Q - 81795705 =========================================>  
//                    * MSN - bndy5337@msn.com *  
//                    * * * * * * * * * * * * *   
//  
// ------------------------------------------------------------------------------
// JS 分页函数
// 适合Ajax分页时使用
// ==============================================================================
var pager;
var Pager = function(totalPageCount, pageSize) {
    this.pageSize = pageSize;
    this.currentPage = 11;
    this.totalPageCount = parseInt(totalPageCount);
    var ele;
    var handler;
    this.getBeginPageNum = function() {
        if (this.pageSize &gt; this.totalPageCount) {
            return 1;
        }
        if (this.currentPage &gt; this.totalPageCount) {
            return this.totalPageCount - this.pageSize + 1;
        }
        else {
            return Math.floor((this.currentPage - 1) / this.pageSize) * this.pageSize + 1;
        }
    };
    this.getEndPageNum = function() {
        var x = Math.floor((this.currentPage - 1) / this.pageSize) * this.pageSize + this.pageSize;
        if (this.pageSize &gt; this.totalPageCount || this.currentPage &gt; this.totalPageCount || x &gt; this.totalPageCount) {
            return this.totalPageCount;
        }
        return x;
    };
    this.init = function(htmlElementName, handlerMethod) {
        pager = this;
        ele = htmlElementName;
        handler = handlerMethod;
        var beginPageNum = this.getBeginPageNum();
        var endPageNum = this.getEndPageNum();
        var str = &quot;&quot;;
        if (this.currentPage &gt; this.pageSize) {
            str = &quot; &lt;span onclick='pager.changePage(&quot; + (beginPageNum - 1) + &quot;)'&gt; &lt;&lt; &lt;/span&gt;  &quot;;
        }
        for (var i = beginPageNum; i &lt;= endPageNum; i++) {
            str = str + &quot; &lt;span onclick='pager.changePage(&quot; + i + &quot;)'&gt;&quot; + i.toString() + &quot;&lt;/span&gt; &quot;;
        }
        if (totalPageCount &gt; endPageNum) {
            var nextPageBeginNum = endPageNum + 1 &lt; this.totalPageCount ? endPageNum + 1 : this.totalPageCount;
            str = str + &quot; &lt;span onclick='pager.changePage(&quot; + nextPageBeginNum + &quot;)'&gt; &gt;&gt; &lt;/span&gt;  &quot;;
        }
        document.getElementById(ele).innerHTML = &quot;&quot;;
        document.getElementById(ele).innerHTML = str;
    };
    this.changePage = function(currentPage) {
        this.currentPage = currentPage;
        this.init(ele, handler);
    };
}